//
//  JWLoginViewController.m
//  见闻新闻
//
//  Created by brother on 16/3/7.
//  Copyright © 2016年 JoanBoat. All rights reserved.
//

#import "JWLoginViewController.h"

#import <AFNetworking.h>

#import "JWSideViewController.h"

#import "AppDelegate.h"

#import <MBProgressHUD.h>

#import "JWUserModel.h"

#import <MJExtension.h>

@interface JWLoginViewController ()<UIAlertViewDelegate>

@property (weak, nonatomic) IBOutlet UITextField *userNameText;

@property (weak, nonatomic) IBOutlet UITextField *passWordText;

@property (nonatomic,strong)NSMutableDictionary *dic;

@end



@implementation JWLoginViewController


-(NSMutableDictionary *)dic {

    if (_dic == nil) {
        _dic = [NSMutableDictionary dictionary];
    }
    return _dic;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    NSLog(@"%d",appDelegate.isLogin);
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = NO;
    
}


-(void)Login{

    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    
    NSSet * contentTypes = manger.responseSerializer.acceptableContentTypes;
    NSMutableSet * newContentTypes = [NSMutableSet setWithSet:contentTypes];
    [newContentTypes addObject:@"text/html"];
    manger.responseSerializer.acceptableContentTypes = newContentTypes;
    
    //测试账号
    NSDictionary *parameters = @{@"info" : @"123",
                                 @"password" :@"123"};
    
    //获取输入框内容 暂时盖住
//    NSDictionary *parameters = @{@"info" : self.userNameText.text,
//                                @"password" : self.passWordText.text};
    
    NSString *userLogin = @"http://zhixinnews.applinzi.com/Home/User/userlogin";
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    hud.labelText = @"正在登陆中……";
    
   [manger POST:userLogin parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       if ([responseObject[@"code"]integerValue] == 1) {
            [[[UIAlertView alloc] initWithTitle:@"提示" message:@"登陆成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil] show];
           AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
           
           appDelegate.isLogin = YES;
           
           self.dic = responseObject[@"data"];
           
           
     NSUserDefaults *UserDefaults = [NSUserDefaults standardUserDefaults];
           
    [UserDefaults setObject:self.dic forKey:UserInfo];
           
    [UserDefaults synchronize];
           
    hud.hidden = YES;
       
       }else{
           
       [[[UIAlertView alloc] initWithTitle:@"提示" message:responseObject[@"data"] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]show];
           
          hud.hidden = YES;
         
      
       }
   } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
       dispatch_async(dispatch_get_main_queue(), ^{
           hud.hidden = YES;
       });
       
       NSLog(@"%@",error);
   }];
    
    
}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 0) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
}


- (IBAction)LoginClick:(id)sender {
    // 测试 暂时盖住
    
//    if ([self.userNameText.text isEqualToString:@""]||[self.passWordText.text isEqualToString:@""]) {
//        [[[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名或密码不能为空" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]show];
//    }else  {
    
        [self Login];
    //}

}





- (IBAction)backToRoot:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)dealloc{


}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
